import UIKit


let placeImage = UIImage(named: "place_image")

@objcMembers
class TAMTAppUtils {
    
    

    // MARK: - Singleton
    static let shared = TAMTAppUtils()

    private init() {}

    // MARK: - Screen Dimensions
    var screenWidth: CGFloat {
        return UIScreen.main.bounds.width
    }
    
    var mainColor:UIColor {
        return UIColor(red: 245/255.0, green: 51/255.0, blue: 42/255.0, alpha: 1)
    }

    var screenHeight: CGFloat {
        return UIScreen.main.bounds.height
    }

    // MARK: - Safe Area Insets
    var safeAreaInsets: UIEdgeInsets {
        guard let window = UIApplication.shared.windows.first else {
            return .zero
        }
        return window.safeAreaInsets
    }

    // MARK: - Status Bar Height
    var statusBarHeight: CGFloat {
        if #available(iOS 13.0, *) {
            let statusBarFrame = UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame
            return statusBarFrame?.height ?? 0
        } else {
            return UIApplication.shared.statusBarFrame.height
        }
    }
    
    var navHeight:CGFloat {
        return statusBarHeight + 44.0
    }

    // MARK: - Is Notch Screen
    var isNotchScreen: Bool {
        // 判断顶部安全区域高度是否大于20以检测刘海屏
        return safeAreaInsets.top > 20
    }
    
    var random: UIColor {
        let red = CGFloat.random(in: 0...1)
        let green = CGFloat.random(in: 0...1)
        let blue = CGFloat.random(in: 0...1)
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
    
    // 请求 API 的方法，并返回 JSON 数据
    static func appRequestAPI(api: String, completion: @escaping ([String: Any]?, Error?) -> Void) {
        // URL 字符串
        let urlString = api
        
        // 创建 URL 对象
        guard let url = URL(string: urlString) else {
            print("Invalid URL")
            completion(nil, NSError(domain: "InvalidURL", code: -1, userInfo: nil))
            return
        }
        
        // 创建请求对象
        let request = URLRequest(url: url)
        
        // 创建数据任务
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            // 检查是否有错误发生
            if let error = error {
                print("Error: \(error)")
                completion(nil, error)
                return
            }
            
            // 检查响应状态码
            guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
                print("Error: Invalid response or status code")
                completion(nil, NSError(domain: "InvalidResponse", code: -2, userInfo: nil))
                return
            }
            
            // 将响应数据转换为字典
            guard let data = data else {
                print("Error: No data received")
                completion(nil, NSError(domain: "NoData", code: -3, userInfo: nil))
                return
            }
            
            do {
                // 解析 JSON 数据
                let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                
                completion(json, nil)
            } catch let jsonError {
                print("JSON Error: \(jsonError)")
                completion(nil, jsonError)
            }
        }
        
        // 启动数据任务
        task.resume()
    }
}

